package com.xu.controller;

import com.xu.pojo.Order;
import com.xu.pojo.User;
import com.xu.pojo.VerifyCode;
import com.xu.service.OrderService;
import com.xu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/getAllUser")
    public String getAllUser(Model model)
    {
        List<User> allUser = userService.getAllUser();
        model.addAttribute("allUser", allUser);
        return "admin/user/user";
    }

    @RequestMapping("/register")
    public String register(User user,Model model,String code)
    {

        if (VerifyCode.verifyCode!=null&&VerifyCode.verifyCode.equals(code)){
            if(userService.queryUserById(user.getUid())==null)
            {
                userService.addUser(user);
                model.addAttribute("msg4", "已注册，请登录");
                return "login";
            }
            else
            {
                model.addAttribute("msg1", "已有此用户名");
                return "user/register-1";
            }
        }else {
            model.addAttribute("msg1", "验证码有误");
            return "user/register-1";
        }

    }
    @RequestMapping("/delUser/{uid}/{userid}")
    public String delUser(@PathVariable("uid") int uid,@PathVariable("userid") int userid,Model model)
    {
        System.out.println(orderService.queryOrderByUserId(userid));
        if (orderService.queryOrderByUserId(userid).isEmpty())
        {
            if (userid==1)
            {
                model.addAttribute("msg5", "管理员用户无法删除");
                return "user-error-1";
            }
            else
            {
                System.out.println("执行成功---------------------->"+userService.queryUserById(uid));
                userService.delUser(userid);
                return "redirect:/getAllUser";
            }
        }
        else
        {
            model.addAttribute("msg6", "当前用户存在订单，无法删除");
            return "user-error-2";
        }

    }
    @RequestMapping("/toUpdPwd/{uid}")
    public String toUpdPwd(Model model, @PathVariable("uid") int uid)
    {
        User user = userService.queryUserById(uid);
        model.addAttribute("user", user);
        return "user/user-upd";
    }
    @RequestMapping("/updPwd")
    public String updPwd(int userid, String username, String userpwd, Model model, HttpSession httpSession)
    {
        User user = new User();
        user.setUserid(userid);
        user.setUsername(username);
        user.setUserpwd(userpwd);
        System.out.println("userid---------------->"+user);
        userService.updUser(user);
        httpSession.removeAttribute("user");
        httpSession.removeAttribute("username");
        httpSession.removeAttribute("uid");
        return "redirect:/getAllUser";
    }
    @RequestMapping("/updPwd_user")
    public String updPwd_user(int userid, String username, String userpwd, Model model, HttpSession httpSession)
    {
        User user = new User();
        user.setUserid(userid);
        user.setUsername(username);
        user.setUserpwd(userpwd);
        System.out.println("userid---------------->"+user);
        userService.updUser(user);
        httpSession.removeAttribute("user");
        httpSession.removeAttribute("username");
        httpSession.removeAttribute("uid");
        model.addAttribute("msg2", "修改成功，请重新登录");
        return "login";
    }

    @RequestMapping("toMine")
    public String toMine(Model model,HttpSession httpSession)
    {
        Integer uid = (Integer) httpSession.getAttribute("uid");
        if(uid==null){
            model.addAttribute("msg3", "未登录，请登录后操作");
            return "login";
        }
        List<Order> order = orderService.queryOrderByUserId(uid);
        System.out.println(order);
        model.addAttribute("order", order);
        return "user/mine";
    }

}
